package com.bdzt.beidoubox;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.bdzt.beidoubox.activity.BaseActivity;
import com.bdzt.beidoubox.fragment.HomeFragment;
import com.bdzt.beidoubox.fragment.MessageFragment;
import com.bdzt.beidoubox.fragment.SearchBoxFragment;
import com.bdzt.beidoubox.fragment.SetFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private ViewPager viewPager;
    private RadioGroup rg_bottom;
    private List<Fragment> list;
    private FragmentManager manager;
    private int currenttab = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        manager = this.getSupportFragmentManager();
        setContentView(R.layout.activity_main);
        initView();
        initData();
        changeFragment(0);
        rg_bottom.check(R.id.rb_home);
    }

    private void changeFragment(int i) {
        viewPager.setCurrentItem(i, true);
    }

    public void initData() {
        list = new ArrayList<>();

        list.clear();
        list.add(new HomeFragment());
        list.add(new MessageFragment());
        list.add(new SearchBoxFragment());
        list.add(new SetFragment());

        viewPager.setAdapter(new MyAdapter(manager));
    }

    public void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rg_bottom = (RadioGroup) findViewById(R.id.rg_bottom);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rg_bottom.check(R.id.rb_home);
                }else if (position==1){
                    rg_bottom.check(R.id.rb_message);
                }else if (position==2){
                    rg_bottom.check(R.id.rb_box);
                }else if (position==3){
                    rg_bottom.check(R.id.rb_set);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        rg_bottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int temp = -1;
                if (i == R.id.rb_message) {
                    rg_bottom.check(R.id.rb_message);
                    temp = 1;
                } else if (i == R.id.rb_box) {
                    rg_bottom.check(R.id.rb_box);
                    temp = 2;
                } else if (i == R.id.rb_home) {
                    rg_bottom.check(R.id.rb_home);
                    temp = 0;
                } else if (i == R.id.rb_set) {
                    rg_bottom.check(R.id.rb_set);
                    temp = 3;
                }

                if (temp != -1) {
                    changeFragment(temp);
                }
            }
        });
    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("真的要退出吗？")
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    finish();
                                }

                            })
                    .setNegativeButton("取消",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    dialog.dismiss();
                                }

                            }).show();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
            int currentItem = viewPager.getCurrentItem();
            if (currentItem == currenttab) {
                return;
            }
            currenttab = viewPager.getCurrentItem();
            changeButton(viewPager.getCurrentItem());


        }

        private void changeButton(int currentItem) {
            switch (currentItem) {
                case 0:
                    rg_bottom.check(R.id.rb_home);
                    break;
                case 1:
                    rg_bottom.check(R.id.rb_message);
                    break;
                case 2:
                    rg_bottom.check(R.id.rb_box);
                    break;
                case 3:
                    rg_bottom.check(R.id.rb_set);
                    break;
            }
        }
    }

}
